﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#pragma once

// std headers
#include <vector>
#include <memory>

// qt headers
#include<QString>
#include <QListWidget>
#include <QPushButton>
#include <QComboBox>
#include <QVector>
#include <QMenu>
// gcmp headers

// custom headers
#include "MessageDialog.h"
#include "ILegend.h"
#include "SearchTextBox.h"

namespace gmepgcs
{
    class BottomLineTab;
    class LegendChoosingDialog : public MessageDialog
    {
        Q_OBJECT

    private:
        explicit LegendChoosingDialog(QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags());
        LegendChoosingDialog(const LegendChoosingDialog &) = default;
        LegendChoosingDialog &operator=(const LegendChoosingDialog &) = default;
        virtual ~LegendChoosingDialog();

    private:
        static LegendChoosingDialog *Get(QWidget *parent = nullptr);

    private:
        static LegendChoosingDialog *s_pInstance;

    public:
        enum ContentUIState
        {
            NoItems,
            NoNetWork,
            FromLocal,
            FromPublic,
            FromEnterprise
        };

        static MessageDialog::MessageDialogResult Show(
            const std::wstring &projectId,
            const std::wstring &enterpriseId,
            const std::wstring &categoryCode,
            const std::wstring &categoryUid,
            const bool isPrivateEnv,
            const ILegend::LegendType &legendType,
            std::shared_ptr<ILegend> &chosenLegend,
            QWidget *parent = nullptr);

        public slots:
        void OnLegendItemChanged(QListWidgetItem *current, QListWidgetItem *previous);
        void OnSearchBtnClick();

    private:
        void Init();
        void Resume();
        void ClearLegendListWidget();

        void ShowLocalLegendContent();
        void ShowPublicLegendContent();
        void ShowEnterpriseLegnedContent();

        void FillLocalLegendIntoListWidget();
        void FillPublicLegendIntoListWidget();
        void FillEnterpriseLegendIntoListWidget();

        void FillLegendIntoListWidget(std::vector<std::shared_ptr<ILegend>>);
        void DownLoadThumbnailSetBack(QLabel*, QPixmap);

        void SetCenterContentUI(ContentUIState);
        QLayout * InitContentGuideView(QWidget * parent);

        std::shared_ptr<ILegend> GetChosenLegend();
        void AddChosenLegendToLocal();

        void SetNewtWindowTitle(std::wstring fromLib);

    private:
        QListWidget *m_LegendsListWidget;
        QWidget * m_ContentGuideWidget;
        QLabel* m_ContentImageLabel;
        QLabel* m_ContentText;
        QPushButton * m_LoadInBtn;
        QPushButton * OkBtn;
        SearchTextBox * SearchTxt;
        QMenu* m_LoadInMenu;

    private:
        const int itemWidth = 96;
        ContentUIState UiState;
        std::wstring m_projectId;
        std::wstring m_enterpriseId;
        std::wstring m_categoryCode;
        std::wstring m_categoryUid;
        ILegend::LegendType m_legendType;
        int32_t m_pageSize;

        std::vector<std::shared_ptr<ILegend>> m_vCurrLegends;
        std::vector<std::shared_ptr<ILegend>> m_vLocalLegends;
        std::vector<std::shared_ptr<ILegend>> m_vPublicLegends;
        std::vector<std::shared_ptr<ILegend>> m_vEnterpriseLegends;

        std::shared_ptr<ILegend> m_chosenLegend;

        QVector<QLabel*> m_Thumbnails;

        QString m_errorMessage;

        bool m_isPrivateEnv = false;
        /*   protected:
               virtual bool eventFilter(QObject *obj, QEvent *event) override;*/
    };
}